/**
 * 状态配置API接口
 * 从后端获取各种状态枚举配置
 */
import { request } from '@/utils/request'

export interface StatusOption {
  value: string
  label: string
  color: string
}

// 获取任务状态选项
export const getTaskStatusOptions = () => {
  return request.get<{ data: StatusOption[] }>('/status/status/task')
}

// 获取需求状态选项
export const getRequirementStatusOptions = () => {
  return request.get<{ data: StatusOption[] }>('/status/status/requirement')
}

// 获取缺陷状态选项
export const getDefectStatusOptions = () => {
  return request.get<{ data: StatusOption[] }>('/status/status/defect')
}

// 获取项目状态选项
export const getProjectStatusOptions = () => {
  return request.get<{ data: StatusOption[] }>('/status/status/project')
}

// 获取版本状态选项
export const getVersionStatusOptions = () => {
  return request.get<{ data: StatusOption[] }>('/status/status/version')
}

// 获取用户状态选项
export const getUserStatusOptions = () => {
  return request.get<{ data: StatusOption[] }>('/status/status/user')
}

// 获取发布状态选项
export const getReleaseStatusOptions = () => {
  return request.get<{ data: StatusOption[] }>('/status/status/release')
}

// 获取优先级选项
export const getPriorityOptions = () => {
  return request.get<{ data: StatusOption[] }>('/status/priority')
}

// 获取严重程度选项
export const getSeverityOptions = () => {
  return request.get<{ data: StatusOption[] }>('/status/severity')
}

// 获取类型选项
export const getTypeOptions = () => {
  return request.get<{ data: { task: StatusOption[], requirement: StatusOption[], defect: StatusOption[] } }>('/status/types')
}

// 获取来源选项
export const getSourceOptions = () => {
  return request.get<{ data: { requirement: StatusOption[], defect: StatusOption[] } }>('/status/sources')
}

// 状态工具类
export class StatusUtils {
  private static cache = new Map<string, StatusOption[]>()

  // 获取状态标签
  static getStatusLabel(type: string, value: string): string {
    const options = this.cache.get(type)
    if (options) {
      const option = options.find(opt => opt.value === value)
      return option?.label || value
    }
    return value
  }

  // 获取状态颜色
  static getStatusColor(type: string, value: string): string {
    const options = this.cache.get(type)
    if (options) {
      const option = options.find(opt => opt.value === value)
      return option?.color || 'default'
    }
    return 'default'
  }

  // 获取状态类型
  static getStatusType(color: string): string {
    const colorMap: Record<string, string> = {
      'green': 'success',
      'blue': 'primary',
      'orange': 'warning',
      'red': 'danger',
      'purple': 'info',
      'gray': 'secondary',
      'yellow': 'warning',
      'cyan': 'info',
      'pink': 'info',
      'brown': 'secondary'
    }
    return colorMap[color] || 'default'
  }

  // 缓存状态选项
  static cacheOptions(type: string, options: StatusOption[]) {
    this.cache.set(type, options)
  }

  // 清除缓存
  static clearCache() {
    this.cache.clear()
  }
}

export default {
  getTaskStatusOptions,
  getRequirementStatusOptions,
  getDefectStatusOptions,
  getProjectStatusOptions,
  getVersionStatusOptions,
  getUserStatusOptions,
  getReleaseStatusOptions,
  getPriorityOptions,
  getSeverityOptions,
  getTypeOptions,
  getSourceOptions,
  StatusUtils
}